/**
 * 
 * @param {string} str // name @ httpUrls 
 * @returns Promise
 */
function promiseTransfer(str) {
    // str = 'lib_app@http://localhost:3000/remoteEntry.js'
    const arr = str.split("@");
    const name = arr[0];
    const httpUrls = arr[1];
   return `promise new Promise(resolve => {
    // const urlParams = new URLSearchParams(window.location.search)
    // const version = urlParams.get('app1VersionParam')
    // This part depends on how you plan on hosting and versioning your federated modules
    const remoteUrlWithVersion = '${httpUrls}'
    const script = document.createElement('script')
    script.src = remoteUrlWithVersion
    script.onload = () => {
        // the injected script has loaded and is available on window
        // we can now resolve this Promise
        const proxy = {
        get: (request) => window['${name}'].get(request),
        init: (arg) => {
            try {
            return window['${name}'].init(arg)
            } catch(e) {
            console.log('remote container already initialized')
            }
        }
        }
        resolve(proxy)
    }
    // inject this script with the src set to the versioned remoteEntry.js
    document.head.appendChild(script);
    })
    `
}

module.exports = promiseTransfer